package com.hui.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.admin.common.UserContext;
import com.hui.admin.common.enums.ErrorCodeEnum;
import com.hui.admin.common.exception.UserException;
import com.hui.admin.dto.request.UserProfileRequestDTO;
import com.hui.admin.dto.response.UserProfileResponseDTO;
import com.hui.admin.entity.UserDO;
import com.hui.admin.entity.UserProfileDO;
import com.hui.admin.mapper.UserMapper;
import com.hui.admin.mapper.UserProfileMapper;
import com.hui.admin.service.UserProfileService;
import com.hui.admin.utils.CityPinyinToChineseUtil;
import com.hui.admin.utils.ProvincePinyinToChineseUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 用户信息服务实现层
 */
@Service
public class UserProfileServiceImpl extends ServiceImpl<UserProfileMapper, UserProfileDO> implements UserProfileService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void updateUserProfile(UserProfileRequestDTO dto) {
        Long currentUserId = UserContext.getCurrentUserId();
        LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserDO::getId, currentUserId);
        UserDO user = userMapper.selectOne(queryWrapper);
        if (user == null) {
            throw new UserException(ErrorCodeEnum.USER_NOT_EXISTS);
        }
        LambdaQueryWrapper<UserProfileDO> queryWrapperUserProfile = new LambdaQueryWrapper<>();
        queryWrapperUserProfile.eq(UserProfileDO::getUserId,currentUserId);
        UserProfileDO profile = baseMapper.selectOne(queryWrapperUserProfile);

        profile.setName(dto.getName());
        profile.setSpecialSkills(String.join(",", dto.getSpecialSkills()));
        profile.setGender(dto.getGender());
        profile.setCompany(dto.getCompany());
        profile.setJobTitle(dto.getJobTitle());
        profile.setQq(dto.getQq());
        profile.setWechat(dto.getWechat());
        profile.setBlog(dto.getBlog());
        profile.setIntroduction(dto.getIntroduction());
        profile.setLocationProvince(ProvincePinyinToChineseUtil.getProvinceNameByPinyin(dto.getProvince()));
        profile.setLocationCity(CityPinyinToChineseUtil.getAreaNameByPinyin(dto.getProvince(), dto.getCity()));

        // 修改用户的姓名
        if (dto.getName() != null) {
            user.setName(dto.getName());
            userMapper.updateById(user);
        }

        updateById(profile);
    }

    @Override
    public UserProfileResponseDTO getUserProfileById(Long userId) {
        LambdaQueryWrapper<UserProfileDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserProfileDO::getUserId, userId);
        UserProfileDO userProfileDO = getOne(queryWrapper);
        if (userProfileDO == null) {
            throw new UserException(ErrorCodeEnum.USER_NOT_EXISTS);
        }

        UserProfileResponseDTO result = BeanUtil.copyProperties(userProfileDO, UserProfileResponseDTO.class);

        String provincePinyinByCityAndArea = CityPinyinToChineseUtil.getProvincePinyinByCityAndArea(userProfileDO.getLocationProvince(), userProfileDO.getLocationCity());
        String areaPinyinByName = CityPinyinToChineseUtil.getAreaPinyinByName(provincePinyinByCityAndArea, userProfileDO.getLocationCity());
        result.setProvince(provincePinyinByCityAndArea);
        result.setCity(areaPinyinByName);

        return result;
    }
}
